home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / qdUtils.c < prev    next >
Encoding:
Text File  |  1998-09-09  |  5.7 KB  |  205 lines  |  [TEXT/CWIE]

  1. // Useful utilities for working with pictures
  2. //
  3. // David Hayward 
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyrite 1995, Apple Computer,Inc
  8. // 
  9. // Routines for reading and writing pictures 
  10. // and for drawing pictures using bottlenecks 
  11. //
  12. // 12/13/94    david    first cut
  13. // 9/19/95    david    improved comments
  14.  
  15.  
  16. #include "qdUtils.h"
  17.  
  18.  
  19. /**\
  20. |**| ==============================================================================
  21. |**| PUBLIC FUNCTIONS
  22. |**| ==============================================================================
  23. \**/
  24.  
  25.  
  26. /*------------------------------------------------------------------------------*\
  27.     DoReadPICT
  28.  *------------------------------------------------------------------------------*
  29.         This reads a picture file from a FSSpec into a PicHandle
  30. \*------------------------------------------------------------------------------*/
  31. OSErr DoReadPICT ( FSSpec spec, PicHandle *pict ) 
  32. {
  33.     long            fileSize ;
  34.     short            ref ;
  35.     OSErr            err ;
  36.     
  37.     // try to open the file
  38.     err = FSpOpenDF( &spec, fsRdPerm, &ref ) ;
  39.     if ( err ) return nil;
  40.  
  41.     // pict files have a 512 byte header at the front - we dont care about this
  42.     // we can find the size of the pict by subtracting 512 bytes from the length
  43.     // of the file.  We then want to size a handle to that and read the data
  44.     // into the handle.
  45.     
  46.     err = GetEOF( ref, &fileSize ) ;
  47.     if ( err ) goto bail;
  48.     
  49.     err = SetFPos( ref, fsFromStart, 512) ;
  50.     if ( err ) goto bail;
  51.  
  52.     fileSize -= 512 ;
  53.     
  54.     *pict = (PicHandle)NewHandle( fileSize ) ;
  55.     if( *pict == nil )
  56.     {
  57.         err = MemError() ;
  58.         goto bail;
  59.     }
  60.     
  61.     HLock( (Handle)*pict ) ;
  62.     err = FSRead( ref, &fileSize, (Ptr)**pict ) ;
  63.     HUnlock( (Handle)*pict ) ;
  64.     if( err )
  65.     {
  66.         DisposeHandle( (Handle)*pict ) ;
  67.         *pict = nil ;
  68.     }
  69.  
  70. bail:
  71.     FSClose( ref ) ;
  72.     return err ;    
  73. }
  74.  
  75.  
  76. /*------------------------------------------------------------------------------*\
  77.     DoWritePICT
  78.  *------------------------------------------------------------------------------*
  79.         This writes a picture file to a FSSpec from a PicHandle
  80.         If the file referenced by the FSSpec already exists, 
  81.         then it is deleted and a new file is created.
  82. \*------------------------------------------------------------------------------*/
  83. OSErr DoWritePICT ( FSSpec spec, PicHandle pict, OSType creator )
  84. {
  85.     short            refNum, count;
  86.     long            inOutCount;
  87.     unsigned char    header[512];
  88.     OSErr            err ;
  89.  
  90.     for ( count=0; count<512; count++ )
  91.         header[count] = 0x00;
  92.     
  93.     err = FSpCreate( &spec, creator, 'PICT', 0 /*reply.sfScript*/ ) ;
  94.     
  95.     /* this is quick 'n' dirty or there'd be more robust handling here */
  96.     if (err == dupFNErr)
  97.     {
  98.         err = FSpDelete( &spec ) ;
  99.         err = FSpCreate( &spec, creator, 'PICT', 0 /*reply.sfScript*/ ) ;
  100.     }
  101.     
  102.     err = FSpOpenDF( &spec, fsWrPerm, &refNum) ;
  103.  
  104.     if ( err == noErr )
  105.     { 
  106.         inOutCount = 512;
  107.         err = FSWrite( refNum, &inOutCount, header) ;    /* write the header */
  108.         
  109.         if (err == noErr)                                /* don't write if error  */
  110.         {
  111.             inOutCount = GetHandleSize( (Handle)pict ) ;
  112.             HLock((Handle)pict) ;
  113.             err = FSWrite( refNum, &inOutCount, *pict ) ;
  114.             HUnlock( (Handle)pict ) ;
  115.         }
  116.         FSClose( refNum ) ;                                /* close it */
  117.     }
  118.         
  119.     return err ;
  120. }
  121.  
  122.  
  123. /*------------------------------------------------------------------------------*\
  124.     NewSmallGWorld
  125.  *------------------------------------------------------------------------------*
  126.         This creates a new offscreen GWorld that is only 2pixels x 2pixels x 1bit
  127.         This is sufficient if all you need to do is draw into the offscreen
  128.         using passive QuickDraw bottleneck procs.
  129. \*------------------------------------------------------------------------------*/
  130. QDErr NewSmallGWorld ( GWorldPtr *offscreen )
  131. {
  132.     Rect rect = { 0, 0, 2, 2 } ;
  133.     return NewGWorld( offscreen, 1, &rect, nil, nil, nil ) ;
  134. }
  135.  
  136.  
  137. /*------------------------------------------------------------------------------*\
  138.     NewGWorldClear
  139.  *------------------------------------------------------------------------------*
  140.         This creates a new offscreen GWorld using the same arguments as NewGWorld()
  141.         The only difference is that this routine erases the offscreen to white 
  142.         after creating it.
  143. \*------------------------------------------------------------------------------*/
  144. QDErr NewGWorldClear (    GWorldPtr *offscreen, short depth, const Rect *rect,
  145.                         CTabHandle cTable, GDHandle aGDevice, GWorldFlags flags )
  146. {
  147.     QDErr        err ;
  148.     CGrafPtr    savePort;
  149.     GDHandle    saveGDev;
  150.  
  151.     err = NewGWorld( offscreen, depth, rect, cTable, aGDevice, flags ) ;
  152.     if (err) return err ;
  153.                      
  154.     GetGWorld( &savePort, &saveGDev ) ;
  155.     SetGWorld( *offscreen, nil ) ;
  156.     EraseRect( rect ) ;
  157.     SetGWorld( savePort, saveGDev ) ;
  158.     return err ;
  159. }
  160.  
  161.  
  162. /*------------------------------------------------------------------------------*\
  163.     DrawPicHandleUsingBottlenecks
  164.  *------------------------------------------------------------------------------*
  165.         This creates draws a pictute into CGrafPort using the bottleneck supplied
  166.         in the CQDProcs parameter.  If the port parameter is nil, then this
  167.         routine temporarily creates a small offscreen GWorld to draw into.
  168. \*------------------------------------------------------------------------------*/
  169. OSErr DrawPicHandleUsingBottlenecks ( PicHandle pict, CQDProcs procs, CGrafPtr port )
  170. {
  171.     OSErr            err = noErr ;
  172.     CGrafPtr        savePort;
  173.     GDHandle        saveGDev;
  174.     GWorldPtr        offscreen;
  175.     Rect            pictRect;
  176.     PixMapHandle    pixmap;
  177.     
  178.     if ( port==nil )
  179.     {
  180.         err = NewSmallGWorld( &offscreen ) ;
  181.         if (err) return err ;
  182.     }
  183.     else
  184.         offscreen = port ;
  185.  
  186.     
  187.     offscreen->grafProcs = &procs ;            // must be a color port
  188.     
  189.     GetGWorld( &savePort, &saveGDev ) ;
  190.     SetGWorld( offscreen, nil ) ;
  191.  
  192.     pictRect = (**pict).picFrame ;
  193.     
  194.     pixmap = GetGWorldPixMap(offscreen);
  195.     if(LockPixels(pixmap))
  196.         DrawPicture( pict, &pictRect ) ;
  197.     UnlockPixels(pixmap);
  198.     
  199.     SetGWorld( savePort, saveGDev ) ;
  200.     
  201.     if ( port==nil )
  202.         DisposeGWorld( offscreen ) ;
  203.  
  204.     return err ;
  205. }